
<h1>Import Users</h1>

<?php
		if(isset($error))
		{
				echo $error;
		}
?>

	<p>


	<?php
			if(isset($file_items))
			{
	?>
	
	<p>Select corresponding columns below to relate data.</p>
	
	<fieldset>
	<legend>Map CSV columns to users data</legend>
	<form id="import_form-final" method="post" action="<?=current_url()?>">
	<input type="hidden" name="token" name="token" value="<?=$file_token?>" />
	<style type="text/css">
			<!--
			select option[value="-1"] {
					color:#fff;
					background-color:#ff0000;
			}
			select option[selected="selected"][value="-1"] {
					color:#fff;
					background-color:#ff0000;
			}
			select option[value="-1"]:hover {
					color:#fff;
					background-color:#ff0000;
			}
			-->
	</style>
	<table class="greentable">
		<thead>
			<tr>
				<th>Column #</th>
				<th>CSV Column</th>
				<th>DB table Column&nbsp;</th>
			</tr>
		</thead>
		<tbody>
		<?php
				$this->load->helper('form');
				$i = 0;
				foreach($file_items as $file_item)
				{
						$i++;
						$item 	 = str_replace('"', '', $file_item);
						$closest = '';
						foreach ($sql_cols as $column) {
								if($column=='u_stat') continue;
								if(metaphone($item)==metaphone($column)) 
								{
									// echo "$item (".metaphone($item)."), $column (".metaphone($column).")<br>";
									$closest = $column;
									break;
								}
						}
						if($closest!='')
						{
						?>
						<script type="text/javascript">jQuery(document).ready(function(){jQuery('#col-<?=$i?>').val('<?=$closest?>');});</script>
						<?php
						}
		?>
		<tr <?=$i%2?'class="even"':'class="odd"'?>>
			<td><?=$i?>&nbsp;</td>
			<td><?=$item?$item:'Blank'?>&nbsp;</td>
			<td>
					<?php echo form_dropdown('col-'.$i, $sql_cols, -1, 'id="col-'.$i.'"');?>
			</td>
		</tr>
		<?php
				}
		?>
		<tr>
			<td class="noleftborder">&nbsp;</td>
			<td>Skip CSV header <abbr title="Ignore header line in CSV file">?</abbr>&nbsp;</td>
			<td>
					<input type="radio" id="skip_header-yes" name="skip_header" value="yes" checked="checked"/> Yes
					<input type="radio" id="skip_header-no" name="skip_header" value="no" /> No
			</td>
		</tr>
		<tr>
			<td class="noleftborder">&nbsp;</td>
			<td>Sync with MT4 server <abbr title="Update user info with data from MT4 server">?</abbr>&nbsp;</td>
			<td>
					<input type="radio" id="mt4_sync-yes" name="mt4_sync" value="yes" checked="checked"/> Yes
					<input type="radio" id="mt4_sync-no" name="mt4_sync" value="no" /> No
			</td>
		</tr>
		<tr>
			<td class="noleftborder">&nbsp;</td>
			<td>Skip existing record  <abbr title="Do not override any existing user info">?</abbr>&nbsp;</td>
			<td>
					<input type="radio" id="skip_existing-yes" name="skip_existing" value="yes" checked="checked"/> Yes
					<input type="radio" id="skip_existing-no" name="skip_existing" value="no" /> No
			</td>
		</tr>
		<tr>
			<td class="noleftborder">&nbsp;</td>
			<td>Send signup e-mail<br />when creating a new account <abbr title="Send signup e-mail to new users">?</abbr>&nbsp;</td>
			<td>
					<input type="radio" id="email_notif-yes" name="email_notif" value="yes"/> Yes
					<input type="radio" id="email_notif-no" name="email_notif" value="no" checked="checked"/> No
			</td>
		</tr>
		<tr>
			<td colspan="3" style="background-color:rgb(240,240,240);border-bottom-color:rgb(217,217,217);">&nbsp;</td>
		</tr>
		<tr>
			<td colspan="3" style="background-color:rgb(240,240,240);border-bottom-color:rgb(217,217,217);"><input type="submit" id="btCompleteImport" name="btCompleteImport" value="Import Now!" /></td>
		</tr>
		</tbody>
	</table>
	</form>
	</fieldset>
	<?php }else{ ?>
	
	<form id="import_form" method="post" action="<?=current_url()?>" enctype="multipart/form-data">
	<table cellspacing="2" cellpadding="0" border="0">
		<tr>
			<td>Delimiter</td>
			<td>
				<select id="sep" name="sep">
					<option value=";" selected>Semi-colon (;)</option>
					<option value=",">Comma (,)</option>
					<option value="tab">Tabulation (TAB)</option>
				</select>
			</td>
		</tr>
		<tr>
			<td>CSV File</td>
			<td><input type="file" id="csv" name="csv" /></td>
		</tr>
		<tr>
			<td>&nbsp;</td>
			<td>&nbsp;</td>
		</tr>
		<tr>
			<td>&nbsp;</td>
			<td><input type="submit" id="btImport" name="btImport" value="Continue" /></td>
		</tr>
	</table>
	</form>
	
	<?php } ?>
	
	
	<script type="text/javascript">
			jQuery(document).ready(function(){
					jQuery('#import_form').bind('submit', function(){
							return _validate_import_form();
					});
					jQuery('#import_form-final').bind('submit', function(){
							return _proceed_import();
					});
			});
						
			function _validate_import_form() {
					if(!jQuery('#csv').val())
					{
							alert('Please choose a file!');
							return false;	
					}
					var ext = jQuery('#csv').val().split('.').pop().toLowerCase();
					if(jQuery.inArray(ext, ['csv']) == -1)
					{
							alert('Only CSV file allowed!');
							return false;	
					}
					return true;
			}
	
			function _proceed_import() {
					jQuery.ajax({
							type: "POST",
							url: __base_url+"admin/jx_import_users/",
							cache: false,
							data: jQuery('#import_form-final').serialize()+'&__csrft='+__csrf,
							success: function(msg){
									alert(msg);
							},
							error:function (xhr, ajaxOptions, thrownError){
									alert(xhr.responseText);
							}
					});
					return false;
			}
			
	</script>
	
	<div id="search_results-container">


	</div>
	
	
	
	
	
	